 package handling.world;

 import client.MapleCharacter;
 import handling.channel.ChannelServer;
 import handling.world.messenger.MapleMessenger;
 import handling.world.messenger.MapleMessengerCharacter;
 import handling.world.messenger.MessengerType;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.packet.MessengerPacket;










 public class WorldMessengerService
 {
/*  26 */   private static final Logger log = LogManager.getLogger(WorldMessengerService.class.getName());
   private final Map<Integer, MapleMessenger> messengers;
   private final AtomicInteger runningMessengerId;

   private WorldMessengerService() {
/*  31 */     log.info("正在启动[WorldMessengerService]");
/*  32 */     this.runningMessengerId = new AtomicInteger(1);
/*  33 */     this.messengers = new HashMap<>();
   }

   public static WorldMessengerService getInstance() {
/*  37 */     return SingletonHolder.instance;
   }

   public MapleMessenger createMessenger(MapleMessengerCharacter chrfor, MessengerType type, boolean gm) {
/*  41 */     int messengerid = this.runningMessengerId.getAndIncrement();
/*  42 */     MapleMessenger messenger = new MapleMessenger(messengerid, chrfor, type, gm);
/*  43 */     this.messengers.put(Integer.valueOf(messenger.getId()), messenger);
/*  44 */     return messenger;
   }





   public void declineChat(String target, String nameFrom) {
/*  52 */     MapleCharacter player = WorldFindService.getInstance().findCharacterByName(target);
/*  53 */     if (player != null && 
/*  54 */       player.getMessenger() != null) {
/*  55 */       player.getClient().announce(MessengerPacket.messengerNote(nameFrom, 5, 0));
     }
   }





   public MapleMessenger getMessenger(int messengerId) {
/*  64 */     return this.messengers.get(Integer.valueOf(messengerId));
   }





   public MapleMessenger getRandomMessenger(MessengerType type) {
/*  72 */     for (Map.Entry<Integer, MapleMessenger> ms : this.messengers.entrySet()) {
/*  73 */       MapleMessenger messenger = ms.getValue();
/*  74 */       if (messenger != null && messenger.getType() == type && messenger.getLowestPosition() != -1 && !messenger.isHide()) {
/*  75 */         return messenger;
       }
     }
/*  78 */     return null;
   }





   public MapleMessenger getRandomHideMessenger(MessengerType type) {
/*  86 */     for (Map.Entry<Integer, MapleMessenger> ms : this.messengers.entrySet()) {
/*  87 */       MapleMessenger messenger = ms.getValue();
/*  88 */       if (messenger != null && messenger.getType() == type && messenger.getLowestPosition() != -1 && messenger.isHide()) {
/*  89 */         return messenger;
       }
     }
/*  92 */     return null;
   }




   public void leaveMessenger(int messengerId, MapleMessengerCharacter target) {
/*  99 */     MapleMessenger messenger = getMessenger(messengerId);
/* 100 */     if (messenger == null) {
/* 101 */       throw new IllegalArgumentException("No messenger with the specified messengerid exists");
     }
/* 103 */     int position = messenger.getPositionByName(target.getName());
/* 104 */     messenger.removeMember(target);
/* 105 */     for (MapleMessengerCharacter mmc : messenger.getMembers()) {
/* 106 */       if (mmc != null) {
/* 107 */         MapleCharacter player = WorldFindService.getInstance().findCharacterById(mmc.getId());
/* 108 */         if (player != null) {
/* 109 */           player.getClient().announce(MessengerPacket.removeMessengerPlayer(position));
         }
       }
     }
   }

   public void silentLeaveMessenger(int messengerId, MapleMessengerCharacter target) {
/* 116 */     MapleMessenger messenger = getMessenger(messengerId);
/* 117 */     if (messenger == null) {
/* 118 */       throw new IllegalArgumentException("No messenger with the specified messengerid exists");
     }
/* 120 */     messenger.silentRemoveMember(target);
   }

   public void silentJoinMessenger(int messengerId, MapleMessengerCharacter target) {
/* 124 */     MapleMessenger messenger = getMessenger(messengerId);
/* 125 */     if (messenger == null) {
/* 126 */       throw new IllegalArgumentException("No messenger with the specified messengerid exists");
     }
/* 128 */     messenger.silentAddMember(target);
   }




   public void updateMessenger(int messengerId, String nameFrom, int fromChannel) {
/* 135 */     MapleMessenger messenger = getMessenger(messengerId);
/* 136 */     if (messenger == null) {
/* 137 */       throw new IllegalArgumentException("No messenger with the specified messengerid exists");
     }
/* 139 */     int position = messenger.getPositionByName(nameFrom);
/* 140 */     for (MapleMessengerCharacter mmc : messenger.getMembers()) {
/* 141 */       if (mmc != null && !mmc.getName().equals(nameFrom)) {
/* 142 */         MapleCharacter player = WorldFindService.getInstance().findCharacterByName(mmc.getName());
/* 143 */         if (player != null) {
/* 144 */           MapleCharacter fromplayer = ChannelServer.getInstance(fromChannel).getPlayerStorage().getCharacterByName(nameFrom);
/* 145 */           if (fromplayer != null) {
/* 146 */             player.getClient().announce(MessengerPacket.updateMessengerPlayer(nameFrom, fromplayer, position, fromChannel - 1));
           }
         }
       }
     }
   }




   public void joinMessenger(int messengerId, MapleMessengerCharacter target, String from, int fromChannel) {
/* 157 */     MapleMessenger messenger = getMessenger(messengerId);
/* 158 */     if (messenger == null) {
/* 159 */       throw new IllegalArgumentException("No messenger with the specified messengerid exists");
     }
/* 161 */     messenger.addMember(target);
/* 162 */     int position = messenger.getPositionByName(target.getName());
/* 163 */     for (MapleMessengerCharacter mmc : messenger.getMembers()) {
/* 164 */       if (mmc != null) {
/* 165 */         int mposition = messenger.getPositionByName(mmc.getName());
/* 166 */         MapleCharacter player = WorldFindService.getInstance().findCharacterByName(mmc.getName());
/* 167 */         if (player != null) {
/* 168 */           if (!mmc.getName().equals(from)) {
/* 169 */             MapleCharacter fromplayer = ChannelServer.getInstance(fromChannel).getPlayerStorage().getCharacterByName(from);
/* 170 */             if (fromplayer != null) {
/* 171 */               player.getClient().announce(MessengerPacket.addMessengerPlayer(from, fromplayer, position, fromChannel - 1));
/* 172 */               fromplayer.getClient().announce(MessengerPacket.addMessengerPlayer(player.getName(), player, mposition, mmc.getChannel() - 1));
             }  continue;
           }
/* 175 */           player.getClient().announce(MessengerPacket.joinMessenger(mposition));
         }
       }
     }
   }





   public void messengerChat(int messengerId, String chatText, String namefrom, String postxt) {
/* 186 */     MapleMessenger messenger = getMessenger(messengerId);
/* 187 */     if (messenger == null) {
/* 188 */       throw new IllegalArgumentException("No messenger with the specified messengerid exists");
     }
/* 190 */     for (MapleMessengerCharacter mmc : messenger.getMembers()) {
/* 191 */       if (mmc != null && !mmc.getName().equals(namefrom)) {
/* 192 */         MapleCharacter player = WorldFindService.getInstance().findCharacterByName(mmc.getName());
/* 193 */         if (player != null) {
/* 194 */           player.getClient().announce(MessengerPacket.messengerChat(chatText, postxt));
         }
       }
     }
   }




   public void messengerWhisper(int messengerId, String chatText, String namefrom, int position) {
/* 204 */     MapleMessenger messenger = getMessenger(messengerId);
/* 205 */     if (messenger == null) {
/* 206 */       throw new IllegalArgumentException("No messenger with the specified messengerid exists");
     }
/* 208 */     MapleMessengerCharacter mmc = messenger.getMemberByPos(position);
/* 209 */     if (mmc != null && !mmc.getName().equals(namefrom)) {
/* 210 */       MapleCharacter player = WorldFindService.getInstance().findCharacterByName(mmc.getName());
/* 211 */       if (player != null) {
/* 212 */         player.getClient().announce(MessengerPacket.messengerWhisper(namefrom, chatText));
       }
     }
   }




   public void messengerInvite(String sender, int messengerId, String target, int fromChannel, boolean gm) {
/* 221 */     MapleCharacter fromplayer = ChannelServer.getInstance(fromChannel).getPlayerStorage().getCharacterByName(sender);
/* 222 */     MapleCharacter targetplayer = WorldFindService.getInstance().findCharacterByName(target);
/* 223 */     if (targetplayer != null && fromplayer != null) {
/* 224 */       if (!targetplayer.isIntern() || gm) {
/* 225 */         targetplayer.getClient().announce(MessengerPacket.messengerInvite(sender, messengerId, fromChannel - 1));
/* 226 */         fromplayer.getClient().announce(MessengerPacket.messengerNote(target, 4, 1));
       } else {
/* 228 */         fromplayer.getClient().announce(MessengerPacket.messengerNote(target, 4, 0));
       }
     }
   }

   private static class SingletonHolder
   {
/* 235 */     protected static final WorldMessengerService instance = new WorldMessengerService();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\WorldMessengerService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */